package java8_19;

/**
 * @author oldliu
 * @since 1.0
 */
public class JavaClass {
    String name;
    String dirTeacher;
    String[] stus = new String[5];//最多30个学生

    //增加新生
//    public void add(String newName) {
//        //循环找空位置
//        for (int i = 0; i < stus.length; i++) {
//            if (stus[i] == null) {
//                //空
//                stus[i] = newName;
//                return;
//            }
//        }
//        System.out.println("座位已满，坐不下了！");
//    }
    public boolean add(String newName) {
        //循环找空位置
        for (int i = 0; i < stus.length; i++) {
            if (newName.equals(stus[i])) {
                //System.out.println("有重复");
                return false;
            } else {
                if (stus[i] == null) {
                    //空
                    stus[i] = newName;
                    return true;
                }
            }

        }
        // System.out.println("座位已满，坐不下了！");
        return false;
    }

    //找 name 这个人在不在这个班
    public boolean search(String name) {
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] != null && stus[i].equals(name)) {
                return true;
            }
        }
        return false;
    }

    //在限制的范围内查找name是否存在!!!!!!!!!!!!!
    public boolean search2(String name, int start, int end) {
        for (int i = start; i >= 0 && i < stus.length && i <= end; i++) {
            if (stus[i] != null && stus[i].equals(name)) {
                return true;
            }
        }
        return false;
    }

    //开除
    public boolean fire(String name) {
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] == null) {
                // System.out.println("公司里没这个人");
                return false;
            }
            if (stus[i].equals(name)) {
                for (int j = i; j < stus.length - 1; j++) {
                    stus[j] = stus[j + 1];
                }
                //System.out.println("开除");
                //最后一个栉空
                stus[stus.length - 1] = null;
                return true;
            }
        }
        return false;
    }
    //改名字
    public boolean changeName(String name1, String name2) {
        for (int i = 0; i < stus.length; i++) {
            if (stus[i]!=null &&stus[i].equals(name1)) {
                stus[i] = name2;
                return true;
            }
        }
        return false;
    }

    //显示班级信息
    public void show() {
        System.out.println("班级名是" + name + "，指导老师" + dirTeacher);
        System.out.println("学生信息如下:");
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] == null)
                break;
            System.out.print(stus[i] + " ");
        }
        System.out.println();
    }
}
